package com.pgcomb.poweriot.alarmservice.mq;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.pgcomb.web.dao.AlarmInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * @author zhengheqi
 * @version 1.0.0
 * @Description: 将告警消息发送给mq
 * @Date 2021/7/7 17:25
 */
@Slf4j
@Component
public class AlarmMqttSend {

    @Resource
    RabbitTemplate rabbitTemplate;
    @Resource
    ObjectMapper objectMapper;

    @Value("${alarm.sendMsg.alarmESQueue}")
    private String alarmEsQueue;

    public void sendAlarm2MQ(AlarmInfo alarmSendInfo) {
        try {
            String msg = objectMapper.writeValueAsString(alarmSendInfo);
            log.info("发送报文至设备告警：{}", msg);
            rabbitTemplate.convertAndSend(alarmEsQueue, msg);
        } catch (JsonProcessingException e) {
            log.error("mq send error",e);
        }
    }
}
